package cn.hicard.util;

import java.text.DecimalFormat;

/**
 * 金额的格式转化工具类
 * @author Administrator
 *
 */
public class DataTran {

	private static String AmtFmt = "0.00";
	
	/**
	 * 将金额String型转化为double
	 * @param amt
	 * @return
	 */
	public static double strToDouble(String amt) {

		if(!"".equals(amt)&& amt!= null ){
			return Double.valueOf(amt);
		}else{
			return 0;
		}
	}
	
	/**
	 * 将金额转化成小数点后两位格式
	 * @param amt
	 * @return
	 */
	public static String amtTranShow(double amt) {
		DecimalFormat df = new DecimalFormat(AmtFmt);
		return df.format(amt);
	}
	
	
	/**
	 * String类型的金额格式化为小数点后两位格式
	 * @param args
	 */
	public static String amtStrTranShow(String amt){
		if(amt == null || "0".equals(amt)||"".equals(amt.trim())){
			return "0.00";
		}else{
			
			return amtTranShow(Double.valueOf(amt));
		}
	}
	
	
	
	/**
	 * 将字符串以“分”为单位的金额转为以元为单位两位上数的金额字符串<br/>
	 * @param money
	 * @return
	 */
	public static String convertFormat(String money){	
		DecimalFormat format = new DecimalFormat("0.00"); 
		return format.format(Double.valueOf(money) / 100);
	}
	
	/**
	 * 将long型金额转化为字符串 0.00
	 * @param longAmt
	 * @return
	 */
	public static String longToStr(long longAmt){
		String amtStr = "";
		//if(longAmt!=null){
			Double doubleAmout = (double)longAmt/100;
			amtStr = String.valueOf(doubleAmout);
			
		//}
		return amtStrTranShow(amtStr);
	}
	
	//将字符串转化为long型
	public static long strToLong(String strAmt){
		Double dAmt = 0.0;
		if(strAmt!= null){
			dAmt = Double.valueOf(strAmt)*100;
		}
		long longAmt = new Double(dAmt).longValue();
		return longAmt;
	}
	
	
	public static String shieldingCardno(String cardno){
		if(cardno != null && cardno.length()>10){
			String preStr = cardno.substring(0, 6);
			String sufStr = cardno.substring(cardno.length()-4);
			cardno = preStr + "*******" + sufStr;
		}
		return cardno;
	}
	
	
	public static void main(String[] args) {
		
		System.out.println("hhhhh:"+longToStr(0));
		System.out.println("hhhhh:"+strToLong("56.97"));
	}
	
}
